﻿using DearlerPlatform.Common;
using DearlerPlatform.Common.Token;
using DearlerPlatform.Service.CustomerApp;
using DearlerPlatform.Service.CustomerApp.Dto;
using DearlerPlatform.Service.CustomerApp.Views;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace DearlerPlatform.WebApi.Controllers
{
    /// <summary>
    /// 用户登录控制器
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    public class LoginController : ControllerBase
    {
        private readonly ICustomerService _customerService;
        private readonly IConfiguration _configuration;

        public LoginController(ICustomerService customerService,IConfiguration configuration)
        {
            this._customerService = customerService;
            this._configuration = configuration;
        }
        /// <summary>
        /// 用户登录接口
        /// </summary>
        /// <param name="dto">登录参数</param>
        /// <returns>用户信息</returns>
        [HttpPost]
        public async Task<ActionResult<ApiResult<CustomerLoginVM>>> CheckLogin(CustomerLoginDto dto)
        {
            dto.Password = dto.Password.ToMd5();
            var res = await _customerService.CheckPassword(dto);
            if (res)
            {
                var customer = await _customerService.GetCustomerByNoAsync(dto.CustomerNo);
                var tokenMoble = _configuration.GetSection("Jwt").Get<TokenModelJwt>();
                tokenMoble.UserId = customer.Id;
                tokenMoble.UserName = customer.Customername;
                tokenMoble.Role = "Admin";         
                //长Token
                customer.RefreshToken = JwtHelper.CreateJwtRefresh(tokenMoble);
                customer.AccessToken = JwtHelper.CreateJwtAccess(tokenMoble);
                var apiResult = ApiResult<CustomerLoginVM>.Success("登录成功", customer);
                return Ok(apiResult);
            }
            else
            {
                HttpContext.Response.StatusCode = 400;
                var apiResult = ApiResult<CustomerLoginVM>.Fail("用户名密码错误");
                return Accepted(apiResult);
            }
        }
        /// <summary>
        /// 获取短Token
        /// </summary>
        /// <param name="refreshToken">长Token</param>
        /// <returns>新的短Token</returns>
        [Route("[action]")]
        [HttpPost]
        public  ActionResult<ApiResult<string>> GetAccessToken(string refreshToken)
        {
            var tokenMoble = _configuration.GetSection("Jwt").Get<TokenModelJwt>();
            var resTokenMoble = JwtHelper.SerializeRefreshToken(tokenMoble,refreshToken);
            //验证长Token是否合法
            if(resTokenMoble == null)
            {
                return Ok(ApiResult<string>.Fail("长Token验证失败"));
            }
            //验证用户是否过期
            var customer = _customerService.GetCustomerById(resTokenMoble.UserId);
            //当用户过期
            if(customer == null)
            {
                return Ok(ApiResult<string>.Fail("用户已过期"));
            }
            tokenMoble.UserId = customer.Id;
            tokenMoble.UserName = customer.Customername;
            tokenMoble.Role = "Admin";
            var token = JwtHelper.CreateJwtAccess(tokenMoble);
            return Ok(ApiResult<string>.Success("短Token获取成功",token));
        }
    }
}
